//
//  RCMerchantDetailDataModel.m
//  RocketProject
//
//  Created by chen jiedan on 13-6-26.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCMerchantDetailDataModel.h"
#import "FrameworkRelativeDefines.h"
#import "NSString+NSString_Ex_.h"

@implementation RCMerchantDetailItem

@synthesize title = _title;
@synthesize content = _content;
@synthesize total = _total;
@synthesize userName = _userName;
@synthesize score = _score;
@synthesize dateTime = _dateTime;
@synthesize avatar = _avatar;

- (id) initWithDic:(NSDictionary *)dic
{
    self = [super init];
    if (self) {
        self.title = [dic stringValueForKey:@"title" defaultValue:nil operation:NSStringOperationTypeNone];
        self.content = [dic stringValueForKey:@"content" defaultValue:nil operation:NSStringOperationTypeNone];
        self.total = [dic intValueForKey:@"total" defaultValue:0];
        self.userName = [dic stringValueForKey:@"username" defaultValue:nil operation:NSStringOperationTypeTrim];
        self.score = [dic intValueForKey:@"score" defaultValue:0];
        self.dateTime = [dic stringValueForKey:@"datetime" defaultValue:nil operation:NSStringOperationTypeTrim];
        self.avatar = [dic stringValueForKey:@"avatar" defaultValue:nil operation:NSStringOperationTypeTrim];
        
        self.picArray = [dic arrayValueForKey:@"pics" defaultValue:nil];
        
        NSString *type = [dic stringValueForKey:@"type" defaultValue:nil operation:NSStringOperationTypeNone];
        if ([type isEqualToString:@"phone"]) {
            self.type = kMerchantDetailType_Phone;
        }else if([type isEqualToString:@"address"]){
            self.type = kMerchantDetailType_Address;
        }else if([type isEqualToString:@"order"]){
            self.type = kMerchantDetailType_Order;
        }else if([type isEqualToString:@"orderserve"]){
            self.type = kMerchantDetailType_OrderServer;
        }else if ([type isEqualToString:@"preferential"]){
            self.type = kMerchantDetailType_Preferential;
        }else if ([type isEqualToString:@"recommended"]){
            self.type = kMerchantDetailType_Recommended;
        }else if ([type isEqualToString:@"review"]){
            self.type = kMerchantDetailType_Review;
        }else if ([type isEqualToString:@"signin"]){
            self.type = kMerchantDetailType_Signin;
        }else if ([type isEqualToString:@"youhui"]){
            self.type = kMerchantDetailType_Discount;
        }else if ([type isEqualToString:@"food"]){
            self.type = kMerchantDetailType_Food;
        }else{
            self.type = kMerchantDetailType_Others;
        }

    }
    return self;
}

- (void) dealloc
{
    self.title = nil;
    self.content = nil;
    self.userName = nil;
    self.dateTime = nil;
    self.avatar = nil;
    
    [super dealloc];
}
@end

@implementation RCMerchantDetailDataModel

@synthesize merchantName = _merchantName;
@synthesize merchantDesc = _merchantDesc;
@synthesize merchantPrice = _merchantPrice;
@synthesize merchantDistance = _merchantDistance;
@synthesize merchantDetailItems = _merchantDetailItems;
@synthesize merchantImageUrl = _merchantImageUrl;
@synthesize merchantLat = _merchantLat;
@synthesize merchantLon = _merchantLon;
@synthesize merchantSanitation = _merchantSanitation;
@synthesize merchantScore = _merchantScore;
@synthesize merchantService = _merchantService;
@synthesize merchantEnvironment = _merchantEnvironment;
@synthesize merchantTaste = _merchantTaste;
@synthesize favourited = _favourited;


- (void) dealloc
{
    self.merchantName = nil;
    self.merchantPrice = nil;
    self.merchantDistance = nil;
    self.merchantDetailItems = nil;
    self.merchantDesc = nil;
    self.merchantImageUrl = nil;
    self.merchantLon = nil;
    self.merchantLat = nil;
    self.merchantService = nil;
    self.merchantSanitation = nil;
    self.merchantTaste = nil;
    self.merchantEnvironment = nil;
    [super dealloc];
}


// 将JSON数据解析为数据模型
-(id) parseDataFromJSON:(NSDictionary*)data
{
    // 输入参数判断
    if ( nil == data )
        return nil;
 
    RCMerchantDetailDataModel *retObject = nil;
    retObject = [[RCMerchantDetailDataModel alloc] init];
    if ( nil == retObject )
        return retObject;
    
    
    NSDictionary *dataDic = [data dictionaryValueForKey:@"data" defaultValue:nil];
    
    retObject.merchantName = [dataDic stringValueForKey:@"name" defaultValue:nil operation:NSStringOperationTypeNone];
    retObject.merchantPrice = [dataDic stringValueForKey:@"price" defaultValue:nil operation:NSStringOperationTypeNone];
    retObject.merchantDesc = [dataDic stringValueForKey:@"desc" defaultValue:nil operation:NSStringOperationTypeNone];

    retObject.merchantDistance = [dataDic stringValueForKey:@"distance" defaultValue:nil operation:NSStringOperationTypeNone];
    retObject.merchantImageUrl = [dataDic stringValueForKey:@"picurl" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.merchantLon = [dataDic stringValueForKey:@"lon" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.merchantLat = [dataDic stringValueForKey:@"lat" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.merchantTaste = [dataDic stringValueForKey:@"taste" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.merchantSanitation = [dataDic stringValueForKey:@"sanitation" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.merchantEnvironment = [dataDic stringValueForKey:@"environment" defaultValue:nil operation:NSStringOperationTypeTrim];
    
    retObject.merchantScore = [dataDic intValueForKey:@"score" defaultValue:0];
    retObject.merchantService = [dataDic stringValueForKey:@"service" defaultValue:nil operation:NSStringOperationTypeTrim];
    
    int favourite = [dataDic intValueForKey:@"favorite" defaultValue:0];
    if (favourite == 1) {
        retObject.favourited = YES;
    }else{
        retObject.favourited = NO;
    }
    
    
    NSArray *t = [dataDic arrayValueForKey:@"list" defaultValue:nil];
    NSMutableArray *itemsArray = [[NSMutableArray alloc] init];
    for (NSDictionary *dic in t) {
        RCMerchantDetailItem *item = [[RCMerchantDetailItem alloc] initWithDic:dic];
        [itemsArray addObject:item];
        [item release];
    }
    
    //添加交通等
    
    RCMerchantDetailItem *traffic = [[RCMerchantDetailItem alloc]init];
    traffic.type = kMerchantDetailType_Traffic;
    traffic.content = @"交通、营业时间及其他";
    [itemsArray addObject:traffic];
    [traffic release];
    
    NSInteger otherShop = [dataDic intValueForKey:@"othershop" defaultValue:0];
    if (otherShop == 1) {
        RCMerchantDetailItem *otherShop = [[RCMerchantDetailItem alloc]init];
        otherShop.type = kMerchantDetailType_Othershop;
        otherShop.content = @"其他分店";
        [itemsArray addObject:otherShop];
        [otherShop release];
    }
    retObject.merchantDetailItems = itemsArray;
    [itemsArray release];
    
    return [retObject autorelease];
    
}


@end
